package cn.jhc.booklist;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements OnSelectedBookChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        BookListFragment bookListFragment = new BookListFragment();
        transaction.add(R.id.fragment_container,bookListFragment,"bookList");
        transaction.commit();
    }

    @Override
    public void onSelectedBookChanged(int bookIndex) {
        FragmentManager manager = getFragmentManager();

        FragmentTransaction ft = manager.beginTransaction();
        BookDescFragment descFragment = new BookDescFragment();
        Bundle args = new Bundle();
        args.putInt(BookDescFragment.BOOK_INDEX, bookIndex);
        descFragment.setArguments(args);
        ft.replace(R.id.fragment_container,descFragment,"bookDescription");
        ft.addToBackStack(null);
        ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
        ft.commit();

//        BookDescFragment bookDescFragment = (BookDescFragment)
//                manager.findFragmentById(R.id.fragment_container);
//        if(bookDescFragment != null)
//            bookDescFragment.setBook(bookIndex);

    }
}
